package com.qfjg.controllers;

import com.qfjg.feigns.AuthService;
import com.qfjg.feigns.RoleInfo;
import com.qfjg.myutil.ResulRoleInfo;
import com.qfjg.myutil.ResultData;
import com.qfjg.webcore.auth.UserAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Tag(name = "GetRole", description = "获取角色")
@RestController
@RequestMapping("/${spring.application.name}/role")
public class RoleController {

    private AuthService authService;

    @Autowired
    public RoleController(AuthService authService) {
        this.authService = authService;
    }

    @PostMapping("/getRole")
    @Operation(summary = "获取角色")
    public ResultData getRoleInfo(HttpServletRequest request){
        try{
            UserAuth userAuth = (UserAuth)request.getAttribute("userAuth");
            Map<String,String> tokens=(Map<String,String>)request.getAttribute("tokens");
            List<RoleInfo> childrenroles =  authService.roleSelfAllChildren(tokens.get("ct"),tokens.get("_t"));
            return new ResultData(new ResulRoleInfo(userAuth,childrenroles));
        }catch(Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }




}
